package demo.demo10;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class test08 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class<?> aClass = Class.forName("demo.demo10.User01");

        //获取类的名字
        System.out.println(aClass.getName());
        System.out.println(aClass.getSimpleName());

        //获得类的属性
        System.out.println("===========");
        Field[] fields = aClass.getFields();

        fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        //获得指定属性的值
        Field name = aClass.getDeclaredField("name");
        System.out.println(name);

        //获得类的方法
        System.out.println("===========");
        Method[] methods = aClass.getMethods();
        //获得本类及其父类的全部public的方法
        for (Method method : methods) {
            System.out.println("正常的：" + method);
        }
        methods = aClass.getDeclaredMethods();
        //获得本类的所有方法
        for (Method method : methods) {
            System.out.println("getDeclaredMethods" + method);
        }
        //获取指定方法
        //重载
        Method getName = aClass.getMethod("getName", null);
        Method setName = aClass.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);

        //获得指定的构造器
        System.out.println("===========");
        Constructor[] constructors = aClass.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        constructors = aClass.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("#" + constructor);
        }
        //获得指定的构造器
        Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class, int.class, int.class);
        System.out.println("指定" + declaredConstructor);
    }
}
